//
//  SpecificationView.m
//  WuJieLive
//
//  Created by iMac on 2017/12/1.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "SpecificationView.h"
#import "SpecificationCell.h"
#import "SpecificationModel.h"
#import "HeaderCollectionReusableView.h"

@implementation SpecificationView

-(void)awakeFromNib
{
    [super awakeFromNib];
    self.counterView = [[[NSBundle mainBundle] loadNibNamed:@"CounterView" owner:self options:nil] lastObject];
    [self.collectionView registerNib:[UINib nibWithNibName:@"SpecificationCell" bundle:nil] forCellWithReuseIdentifier:@"SpecificationCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self addSubview:self.counterView];
    
    
    [self.counterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.top.equalTo(self.collectionView.mas_bottom);
        make.width.offset(100);
        make.height.offset(40);
    }];
    
    self.currentNum = @"1";
    __weak typeof(self) weakSelf  = self;
    self.counterView.addBlock = ^(UITextField *textField) {
        if ([textField.text integerValue] == [self.maxNum integerValue]) {
            [MBProgressHUD showError:@"没那么多货啦见谅啊" toView:weakSelf];
        }else
        {
            textField.text = [NSString stringWithFormat:@"%ld",[textField.text integerValue] + 1];
            weakSelf.currentNum = textField.text;
        }
    };
    self.counterView.subBlock = ^(UITextField *textField) {
        if ([textField.text integerValue] == 1) {
            [MBProgressHUD showError:@"不能再少了" toView:weakSelf];
        }else
        {
            textField.text = [NSString stringWithFormat:@"%ld",[textField.text integerValue] - 1];
            weakSelf.currentNum = textField.text;
        }
    };
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SpecificationCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SpecificationCell" forIndexPath:indexPath];
    SpecificationModel * model = self.modelArray[indexPath.section];
    [cell.contentBtn setTitle:model.spec_arr[indexPath.row] forState:(UIControlStateNormal)];
    if (model.selectedIndex == indexPath.row) {
        cell.contentView.layer.borderColor = MainColor.CGColor;
        cell.contentBtn.selected = YES;
    }else
    {
        cell.contentBtn.selected = NO;
        cell.contentView.layer.borderColor = TextLessImportColor.CGColor;
    }
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    SpecificationModel * model = self.modelArray[section];
    return model.spec_arr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {

    return CGSizeMake(kScreenWidth, 30);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    
    HeaderCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView" forIndexPath:indexPath];
    SpecificationModel * model = self.modelArray[indexPath.section];
    headView.contentLabel.text = model.spec_name;
    return headView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SpecificationModel * model = self.modelArray[indexPath.section];
    model.selectedIndex = indexPath.row;
    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
    NSInteger count = self.modelArray.count;
    NSInteger times = 0;
    for (int i = 0; i < count; i ++) {
        SpecificationModel * modelTemp = self.modelArray[i];
        if (modelTemp.selectedIndex != -1) {
            times += 1;
        }
        if (times == count) {
            NSString * string = model.spec_arr[model.selectedIndex];
            if (times == 1) {
                for (int i = 0; i < self.detailArray.count; i ++) {
                    DetailGoodsModel * model = self.detailArray[i];
                    if ([string isEqualToString:model.detail_first_name]) {
                        [self bindData:model];
                    }
                }
            }else if (times == 2){
                NSMutableArray * array = [NSMutableArray array];
                SpecificationModel * modelFirst = self.modelArray[0];
                NSString * stringFirst = modelFirst.spec_arr[modelFirst.selectedIndex];
                for (int i = 0; i < self.detailArray.count; i ++) {
                    DetailGoodsModel * model = self.detailArray[i];
                    if ([stringFirst isEqualToString:model.detail_first_name]) {
                        [array addObject:model];
                    }
                }
                SpecificationModel * modelSecond = self.modelArray[1];
                NSString * stringSecond = modelSecond.spec_arr[modelSecond.selectedIndex];
                for (int i = 0; i < array.count; i ++) {
                    DetailGoodsModel * model = array[i];
                    if ([stringSecond isEqualToString:model.detail_second_name]) {
                        [self bindData:model];
                    }
                }
            }
        }
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.modelArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenWidth / 4 , 30);
}

#pragma mark 绑定数据
-(void)bindData:(DetailGoodsModel *)model
{
    self.finalModel = model;
    self.maxNum = model.spec_inventory;
    if (model.price) {
        NSString * tempString = [NSString stringWithFormat:@"¥%@",model.price];
        self.oldPriceLabel.attributedText = [[NSMutableAttributedString alloc]initWithString:tempString attributes:@{NSFontAttributeName:FontSize(13),NSForegroundColorAttributeName:ColorWithHexString(@"333333"),NSStrikethroughStyleAttributeName:@1}];
    }
    if (model.price_coupon) {
        self.priceLabel.text = [NSString stringWithFormat:@"¥%@",model.price_coupon];
    }
    if (model.spec_inventory) {
        self.numberLabel.text = [NSString stringWithFormat:@"库存:%@",model.spec_inventory];
    }
    if (model.spec_picture) {
        [self.headerImage sd_setImageWithURL:[NSURL URLWithString:model.spec_picture] placeholderImage:PlaceImage];
    }
}
//这里是立即购买的操作  buy写成了bug 
- (IBAction)bugAction:(UIButton *)sender {
    __weak typeof (self) weakSelf = self;
    if (self.buyNowBlock) {
        self.buyNowBlock(weakSelf.finalModel.goods_spec_id, weakSelf.currentNum);
    }
}

- (IBAction)getInCartAction:(UIButton *)sender {
    __weak typeof(self) weakSelf = self;
    if (self.takeInCartBlock) {
        self.takeInCartBlock(weakSelf.finalModel, weakSelf.currentNum);
    }
}

- (IBAction)colseAction:(UIButton *)sender {
    if (self.closeBlock) {
        self.closeBlock();
    }
}


@end
